
package com.wyzz.hr.action;

import java.util.*;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.IClientBO;
import com.wyzz.hr.bo.IEmpQuitBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empquit;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;

public class QuitManageAction extends BaseAction {

	public QuitManageAction() {
	}

	public String execute() throws Exception {
		if (null == qemp || StringUtils.isEmpty(qemp.getIds())) {
			addErrorInfo("\u8BF7\u6C42\u7684\u53C2\u6570\u9519\u8BEF,\u8BF7\u9009\u62E9\u8981\u590D\u804C\u7684\u5458\u5DE5\uFF01");
			return "success";
		}
		String ids[] = qemp.getIds().split(",");
		int insertNum = ids.length;
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		String info = posBo.judgePostion(empquit.getPosition().getId(), Integer
				.valueOf(insertNum));
		if (!"SUCC".equals(info)) {
			addActionError(info);
			return "error";
		}
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		Employee tmp = null;
		String empNo = getCurrentEmpNo();
		List errors = new ArrayList();
		IEmpQuitBo empQuitBo = (IEmpQuitBo) getBean("empQuitBo");
		for (int i = 0; i < insertNum; i++) {
			tmp = empBo.loadEmp(ids[i].trim(), null);
			if (null == tmp || tmp.getEmpStatus().intValue() != 0)
				continue;
			empquit.setEmployee(tmp);
			empquit.setEqDate(qemp.getEmpJoinDate());
			empquit.setEqCreateBy(empNo);
			empquit.setEqCreateDate(new Date());
			empquit.setEqLastChangeBy(empNo);
			empquit.setEqLastChangeTime(new Date());
			String msg = empQuitBo.saveEmpQuit(empquit);
			if (!"success".equalsIgnoreCase(msg))
				errors.add(msg);
		}

		if (errors.isEmpty())
			addSuccessInfo("\u6279\u91CF\u590D\u804C\u5458\u5DE5\u6210\u529F\uFF01");
		else
			addErrorInfo(errors);
		return "success";
	}

	public String batchQuit() throws Exception {
		if (null == qemp || StringUtils.isEmpty(qemp.getIds())) {
			addErrorInfo("\u8BF7\u6C42\u7684\u53C2\u6570\u9519\u8BEF,\u8BF7\u9009\u62E9\u8981\u79BB\u804C\u7684\u5458\u5DE5\uFF01");// 请求的参数错误
			// ,
			// 请选择要离职的员工
			// ！
			return "success";
		}
		String currentEmpNo = getCurrentEmpNo();
		String ids[] = qemp.getIds().split(",");
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		Employee tmp = null;
		List errors = new ArrayList();
		List tmpList = null;
		IEmpQuitBo empQuitBo = (IEmpQuitBo) getBean("empQuitBo");
		UserBo userBo = (UserBo) getBean("userService");
		for (int i = 0; i < ids.length; i++) {
			if (ids[i].equals(currentEmpNo)) {
				errors.add("\u4E0D\u80FD\u5C06\u672C\u4EBA\u79BB\u804C\uFF01");
				continue;
			}
			tmp = empBo.loadEmp(ids[i].trim(), null);
			if (null == tmp || tmp.getEmpStatus().intValue() != 1)
				continue;
			Userinfo oldUserinfo = userBo.getUserById(tmp.getId());
			if (userBo.checkAuthModule(oldUserinfo, "911")) {
				errors
						.add((new StringBuilder())
								.append("\u5458\u5DE5")
								.append(tmp.getEmpName())
								.append(
										"\u4E3A\u7CFB\u7EDF\u7BA1\u7406\u5458\uFF0C\u8BF7\u5148\u5230\u7CFB\u7EDF\u6A21\u5757\u53D6\u6D88\u8BE5\u7528\u6237\u7684\u7BA1\u7406\u5458\u6743\u9650\uFF0C\u65B9\u53EF\u79BB\u804C\uFF01")
								.toString());
			} else {
				empquit.setEmployee(tmp);
				empquit.setEqCreateBy(currentEmpNo);
				empquit.setEqCreateDate(new Date());
				empquit.setEqLastChangeBy(currentEmpNo);
				empquit.setEqLastChangeTime(new Date());
				String msg = empQuitBo.saveEmpQuit(empquit);
				if (!"success".equalsIgnoreCase(msg))
					errors.add(msg);
			}
		}

		if (errors.isEmpty())
			addSuccessInfo("\u6279\u91CF\u79BB\u804C\u6210\u529F\uFF01");
		else
			addErrorInfo(errors);
		qemp.setEmpStatus(Integer.valueOf(1));
		return "success";
	}

	public String batchDelete() throws Exception {
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		List errors = empBo.batchUpdateOrDel(qemp, "delete", getCurrentEmpNo());
		if (errors != null && errors.size() > 0)
			addErrorInfo(errors);
		else
			addSuccessInfo("\u6279\u91CF\u5220\u9664\u5458\u5DE5\u6210\u529F\uFF01");
		qemp.setEmpStatus(Integer.valueOf(1));
		return "success";
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public Empquit getEmpquit() {
		return empquit;
	}

	public void setEmpquit(Empquit empquit) {
		this.empquit = empquit;
	}

	public Employee getQemp() {
		return qemp;
	}

	public void setQemp(Employee qemp) {
		this.qemp = qemp;
	}

	public Employee getQuitemp() {
		return quitemp;
	}

	public void setQuitemp(Employee quitemp) {
		this.quitemp = quitemp;
	}

	private static final long serialVersionUID = 1L;
	private static final String AUTHMODULE = "911";
	private Employee emp;
	private Empquit empquit;
	private Employee qemp;
	private Employee quitemp;
}
